using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;

namespace Controls
{
    public partial class NumericSpan : UserControl, IComponentConnector
    {
        public delegate void OnValueChanged(object sender, DependencyPropertyChangedEventArgs args);

        public static readonly DependencyProperty RaiseValueChanagingEventProperty = DependencyProperty.Register("RaiseValueChanagingEvent", typeof(bool), typeof(NumericSpan), new UIPropertyMetadata(false));

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(NumericSpan), new UIPropertyMetadata(0.0, OnInternalValueChanged));

        public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(NumericSpan), new UIPropertyMetadata(1.0));

        public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register("StringFormat", typeof(string), typeof(NumericSpan), new PropertyMetadata("0", OnStringFormatChanged));

        public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(NumericSpan), new UIPropertyMetadata(double.MinValue));

        public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(NumericSpan), new UIPropertyMetadata(double.MaxValue));

        public bool RaiseValueChanagingEvent
        {
            get
            {
                return (bool)GetValue(RaiseValueChanagingEventProperty);
            }
            set
            {
                SetValue(RaiseValueChanagingEventProperty, value);
            }
        }

        public double Value
        {
            get
            {
                return (double)GetValue(ValueProperty);
            }
            set
            {
                if (value > MaxValue)
                {
                    value = MaxValue;
                }
                if (value < MinValue)
                {
                    value = MinValue;
                }
                string text = value.ToString(StringFormat);
                if (text != cValue.Text)
                {
                    cValue.Text = text;
                }
                double value2 = Value;
                string text2 = value2.ToString(StringFormat);
                if (!(text2 == text))
                {
                    value = double.Parse(text);
                    SetValue(ValueProperty, value);
                    if (this.ValueChanged != null)
                    {
                        this.ValueChanged(this, new DependencyPropertyChangedEventArgs(ValueProperty, value2, value));
                    }
                }
            }
        }

        public double Increment
        {
            get
            {
                return (double)GetValue(IncrementProperty);
            }
            set
            {
                SetValue(IncrementProperty, value);
            }
        }

        public string StringFormat
        {
            get
            {
                return (string)GetValue(StringFormatProperty);
            }
            set
            {
                SetValue(StringFormatProperty, value);
            }
        }

        public double MinValue
        {
            get
            {
                return (double)GetValue(MinValueProperty);
            }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }

        public double MaxValue
        {
            get
            {
                return (double)GetValue(MaxValueProperty);
            }
            set
            {
                SetValue(MaxValueProperty, value);
            }
        }

        public event OnValueChanged ValueChanged;

        private static void OnInternalValueChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumericSpan numericSpan = sender as NumericSpan;
            if (numericSpan != null)
            {
                numericSpan.cValue.Text = numericSpan.Value.ToString(numericSpan.StringFormat);
            }
        }

        private static void OnStringFormatChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            NumericSpan numericSpan = sender as NumericSpan;
            if (numericSpan != null)
            {
                numericSpan.cValue.Text = numericSpan.Value.ToString(numericSpan.StringFormat);
            }
        }

        public NumericSpan()
        {
            InitializeComponent();
            base.GotFocus += NumericSpan_GotFocus;
            base.Loaded += NumericSpan_Loaded;
            base.MouseWheel += NumericSpan_MouseWheel;
        }

        private void NumericSpan_GotFocus(object sender, RoutedEventArgs e)
        {
            cValue.Focus();
        }

        private void NumericSpan_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (cValue.IsFocused)
            {
                Value += (double)(e.Delta / 120) * Increment;
                e.Handled = true;
            }
        }

        private void NumericSpan_Loaded(object sender, RoutedEventArgs e)
        {
            string text = Value.ToString(StringFormat);
            cValue.Text = text;
        }

        private void Up_Click(object sender, RoutedEventArgs e)
        {
            if (cValue.Text == "-")
            {
                Value = MinValue;
            }
            else if (!cValue.Text.EndsWith(".") && !string.IsNullOrEmpty(cValue.Text))
            {
                double result = 0.0;
                if (!double.TryParse(cValue.Text, out result))
                {
                    Value = Value;
                }
                else
                {
                    Value = result;
                }
            }
            Value += Increment;
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {
            if (cValue.Text == "-")
            {
                Value = MinValue;
            }
            else if (!cValue.Text.EndsWith(".") && !string.IsNullOrEmpty(cValue.Text))
            {
                double result = 0.0;
                if (!double.TryParse(cValue.Text, out result))
                {
                    Value = Value;
                }
                else
                {
                    Value = result;
                }
            }
            Value -= Increment;
        }

        private void cValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            cValue.Text = cValue.Text.Trim();
            if (!(cValue.Text == "-") && !cValue.Text.EndsWith(".") && !string.IsNullOrEmpty(cValue.Text))
            {
                double result = 0.0;
                if (!double.TryParse(cValue.Text, out result))
                {
                    Value = Value;
                }
                else if (RaiseValueChanagingEvent)
                {
                    Value = result;
                }
            }
        }

        private void cValue_LostFocus(object sender, RoutedEventArgs e)
        {
            double result = 0.0;
            Value = (double.TryParse(cValue.Text, out result) ? result : Value);
        }

        private void cValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                double result = 0.0;
                Value = (double.TryParse(cValue.Text, out result) ? result : Value);
            }
        }

        [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
        [DebuggerNonUserCode]
        internal Delegate _CreateDelegate(Type delegateType, string handler)
        {
            return Delegate.CreateDelegate(delegateType, this, handler);
        }
    }
}
